package lib.quasar.widget.box;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

public class TextBox extends AppCompatCheckBox {

    private final int[][] mState = new int[][]{
            new int[]{android.R.attr.state_pressed},
            new int[]{android.R.attr.state_checked},
            new int[]{android.R.attr.state_enabled}
    };

    private final StateListDrawable drawable1 = new StateListDrawable();
    private final GradientDrawable drawable2 = new GradientDrawable();
    private final GradientDrawable drawable3 = new GradientDrawable();
    private final GradientDrawable drawable4 = new GradientDrawable();

    private final GradientDrawable drawable5 = new GradientDrawable();

    public TextBox(Context context) {
        this(context, null, 0);
    }

    public TextBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setClickable(true);
        setButtonDrawable(null);

        //获取所有的自定义属性
        TypedArray a = null;
        try {
            a = context.obtainStyledAttributes(attrs, R.styleable.TextButton);
            final float radius = a.getDimension(R.styleable.TextButton_tb_bg_radius, 0);
            final float stock = a.getDimension(R.styleable.TextButton_tb_stock_size, 0);
            final int color1 = a.getColor(R.styleable.TextButton_tb_bg_color_normal, Color.TRANSPARENT);
            final int color2 = a.getColor(R.styleable.TextButton_tb_stock_color_normal, Color.TRANSPARENT);
            drawable2.setColor(color1);
            drawable2.setCornerRadius(radius);
            drawable2.setStroke((int) stock, color2);

            final int color10 = a.getColor(R.styleable.TextButton_tb_bg_color_unable, Color.GRAY);
            drawable5.setColor(color10);
            drawable5.setCornerRadius(radius);
            drawable5.setStroke((int) stock, color2);

            final int color3 = a.getColor(R.styleable.TextButton_tb_bg_color_press, Color.TRANSPARENT);
            final int color4 = a.getColor(R.styleable.TextButton_tb_stock_color_press, Color.TRANSPARENT);
            drawable3.setColor(color3);
            drawable3.setCornerRadius(radius);
            drawable3.setStroke((int) stock, color4);

            final int color5 = a.getColor(R.styleable.TextButton_tb_bg_color_check, Color.TRANSPARENT);
            final int color6 = a.getColor(R.styleable.TextButton_tb_stock_color_check, Color.TRANSPARENT);
            drawable4.setColor(color5);
            drawable4.setCornerRadius(radius);
            drawable4.setStroke((int) stock, color6);


            final int color7 = a.getColor(R.styleable.TextButton_tb_text_color_check, Color.BLACK);
            final int color8 = a.getColor(R.styleable.TextButton_tb_text_color_press, Color.BLACK);
            final int color9 = a.getColor(R.styleable.TextButton_tb_text_color_normal, Color.BLACK);

            final int[] ints = {color7, color8, color9};
            final ColorStateList colorStateList = new ColorStateList(mState, ints);
            setTextColor(colorStateList);

        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == a) return;
            a.recycle();
        }
    }

    @Override
    protected void onFinishInflate() {

        drawable1.addState(mState[0], drawable3);
        drawable1.addState(mState[1], drawable4);
        drawable1.addState(mState[2], drawable2);
        setBackgroundDrawable(drawable1);

        super.onFinishInflate();
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
        if (clickable) {
            drawable1.addState(mState[0], drawable3);
            drawable1.addState(mState[1], drawable4);
            drawable1.addState(mState[2], drawable2);
            setBackgroundDrawable(drawable1);
        } else {
            setBackgroundDrawable(drawable5);
        }
    }
}
